package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.dao.MenuMapper;
import pers.xx.sbm.entity.Menu;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.MenuService;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;
import pers.xx.sbm.vo.TableTreeDto;

import java.util.Date;
import java.util.List;

/**
 * @author ：xiexing
 * @description：菜单操作Controller
 * @date ：2020/8/23 21:19
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserUtils userUtils;

    @Autowired
    MenuMapper menuMapper;


    /**
     * @description 菜单管理
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/")
    public String menuPage(Model model) {
        return "page/system/menu";
    }

    /**
     * @description 获取菜单列表
     * @author XieXing
     * @date 2020/8/12 14:52
     */
    @RequestMapping("/getMenuList")
    @ResponseBody
    public TableTreeDto getMenusList(Menu menu, Integer page, Integer limit, String menuSearchContent) {
        if (!StringUtils.isEmpty(menuSearchContent)) {
            menu.setMenuName(menuSearchContent);
        }
        TableTreeDto<Object> res = new TableTreeDto<>();
        res.setData(menuService.selectSelective(null).getData());
        return res;
    }

    /**
     * @description 获取菜单树
     * @author XieXing
     * @date 2020/8/30 15:40
     */
    @RequestMapping("/getTreeMenus")
    @ResponseBody
    public List<Menu> getTreeMenus() {
        return menuService.getWrapMenus(null);
    }

    /**
     * @description 保存菜单信息
     * @author XieXing
     * @date 2020/8/23 21:35
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveMenu(Menu menu) {
        User user = userUtils.getCurrentUser();
        menu.setMenuAddby(user.getUserName());
        menu.setNenuAddbyId(user.getId());
        menu.setMenuAddTime(new Date());
        ResResult resResult = menuService.insertSelective(menu);
        resResult.setOptName("保存菜单信息");
        reloadMenu(user.getId());//跟新redis缓存
        return resResult;
    }


    /**
     * @description 更新菜单信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateMenu(Menu menu) {
        User user = userUtils.getCurrentUser();
        menu.setMenuUpdateb(user.getUserName());
        menu.setMenuUpdatebyId(user.getId());
        menu.setMenuUpdateTime(new Date());
        ResResult resResult = menuService.updateByPrimaryKeySelective(menu);//
        resResult.setOptName("更新菜单信息");
        reloadMenu(user.getId());//跟新redis缓存
        return resResult;
    }

    /**
     * @description 更新菜单信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delMenu(@PathVariable(value = "id") Integer id) {
        ResResult resResult = menuService.deleteByPrimaryKey(id);//
        resResult.setOptName("删除菜单信息");
        User user = userUtils.getCurrentUser();
        reloadMenu(user.getId());//跟新redis缓存
        return resResult;
    }


    /**
     * @description 更新用户状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateDataStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        //TODO 从session获取当前操作用户信息
        ResResult resResult = menuService.updateDataStatus(id, status);
        resResult.setOptName("更新菜单状态");
        return resResult;
    }

    /**
     * @description 菜单更新 更新redis缓存
     * @author XieXing
     * @date 2020/12/9 11:20
     */
    private void reloadMenu(Integer userId) {
        ValueOperations<String, List<Menu>> valueOperations = redisTemplate.opsForValue();
        List<Menu> menus = menuMapper.getMenuByUserId(userId);
        valueOperations.set("menus", menus);
    }
}
